<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://www.fckeditor.net/
 * 
 * "Support Open Source software. What about a donation today?"
 * 
 * File Name: frmresourceslist.html
 * 	This page shows all resources available in a folder in the File Browser.
 * 
 * File Authors:
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link href="browser.css" type="text/css" rel="stylesheet" />
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="lib/JSmarty.js"></script>
	<script type="text/javascript" src="lib/JSmarty/Compiler.js"></script>
	<script type="text/javascript">
	smarty = new JSmarty();
	smarty.plugins_dir = ['../lib/plugins'];
	smarty.template_dir = 'templates';
	smarty.left_delimiter = '<{'  ;
	smarty.right_delimiter = '}>' ;
	</script>
	<script type="text/javascript">

var oListManager = new Object() ;

oListManager.Clear = function()
{
	document.body.innerHTML = '' ;
}

oListManager.GetFolderRowHtml = function( folderName, folderPath )
{
	// Build the link to view the folder.
	var sLink = '<a href="#" onclick="OpenFolder(\'' + folderPath.replace( /'/g, '\\\'') + '\');return false;">' ;

	return '<div style="float:left;padding:4 8px;">' +
				sLink +
				'<img alt="" src="images/Folder.gif" width="16" height="16" border="0"></a>' +
				'&nbsp;' +
				sLink + 
				folderName + 
				'</a>' +
		'</div>' ;
/*
	return '<tr>' +
			'<td width="16">' +
				sLink +
				'<img alt="" src="images/Folder.gif" width="16" height="16" border="0"></a>' +
			'</td><td nowrap colspan="2">&nbsp;' +
				sLink + 
				folderName + 
				'</a>' +
		'</td></tr>' ;*/
}

oListManager.GetFileRowHtml = function( fileName, fileUrl, fileSize , fileMtime )
{
	// Date Object
	oMtime = new Date() ;
	oMtime.setTime( fileMtime * 1000 ) ;

	// Build the link to view the folder.
	var sLinkTitle = '' ;
	var sLink = '<a href="#" onclick="OpenFile(\'' + fileUrl.replace( /'/g, '\\\'') + '\');return false;">' ;

	// Get the file icon.
	var sIcon = oIcons.GetIcon( fileName ) ;

	// GIJ begin
	var sExtension = fileName.substr( fileName.lastIndexOf('.') + 1 ).toLowerCase() ;
	if( sExtension == 'gif' || sExtension == 'png' || sExtension == 'jpg' || sExtension == 'jpeg' ) {
		sLinkTitle = '<img alt="" title="' + fileName + '" src="' + fileUrl + '" height="48" border="0" />' ;
	} else {
		sLinkTitle = '<img alt="" src="images/icons/' + sIcon + '.gif" height="48" border="0">&nbsp;' + fileName ;
	}
	return '<div style="float:left;padding:4 8px;"><a href="#" onclick="DeleteImage(\'' + fileName + '\')"><img src="images/FileDelete.gif" alt="Delete" width="16" height="16" border="0" /></a>&nbsp;' + sLink + sLinkTitle + '</a>&nbsp;' + fileSize + ' KB' + oMtime.toLocaleString() + '</div>' ;
	// GIJ end

	return '<tr>' +
			'<td width="16">' +
				sLink + 
				'<img alt="" src="images/icons/' + sIcon + '.gif" width="16" height="16" border="0"></a>' +
			'</td><td>&nbsp;' +
				sLink + 
				fileName + 
				'</a>' +
			'</td><td align="right" nowrap>&nbsp;' +
				fileSize + 
				' KB' +
		'</td></tr>' ;
}

// thx aramaki!
function DeleteFile( fileUrl , ticket )
{
	if ( confirm('Delete OK?') ) {
		oConnector.SendCommand( 'DeleteFile', 'file_url=' + encodeURIComponent(fileUrl) + '&Ticket=' + encodeURIComponent(ticket) , DeleteFileCallBack ) ;
	}
}

function DeleteFileCallBack( fckXml )
{
  if ( oConnector.CheckError( fckXml ) == 0 )
    window.parent.frames['frmResourcesList'].Refresh() ;
}
// end aramaki

function OpenFolder( folderPath )
{
	// Load the resources list for this folder.
	window.parent.frames['frmFolders'].LoadFolders( folderPath ) ;
}

function OpenFile( fileUrl )
{
	window.top.opener.SetUrl( fileUrl ) ;
	window.top.close() ;
	window.top.opener.focus() ;
}

function LoadResources( resourceType, folderPath )
{
	oListManager.Clear() ;
	oConnector.ResourceType = resourceType ;
	oConnector.CurrentFolder = folderPath ;
	oConnector.SendCommand( 'GetFoldersAndFiles', null, GetFoldersAndFilesCallBack ) ;
}

function Refresh()
{
	LoadResources( oConnector.ResourceType, oConnector.CurrentFolder ) ;
}

function GetFoldersAndFilesCallBack( fckXml )
{
	if ( oConnector.CheckError( fckXml ) != 0 )
		return ;

	// Get resource type
	smarty.assign( 'resource_type' , oConnector.ResourceType ) ;

	// Get error and alert()
	var oNode = fckXml.SelectSingleNode( 'Connector/CustomError' ) ;
	if( oNode != null ) {
		alert( oNode.attributes.getNamedItem('message').value ) ;
	}

	// Get ticket
	var oNode = fckXml.SelectSingleNode( 'Connector/Ticket' ) ;
	smarty.assign( 'ticket' , oNode == null ? '' : oNode.attributes.getNamedItem('value').value ) ;

	// Get the current folder path.
	var oNode = fckXml.SelectSingleNode( 'Connector/CurrentFolder' ) ;
	var sCurrentFolderPath	= oNode.attributes.getNamedItem('path').value ;
	var sCurrentFolderUrl	= oNode.attributes.getNamedItem('url').value ;
	smarty.assign( 'current_folder_path' , sCurrentFolderPath ) ;
	smarty.assign( 'current_folder_url' , sCurrentFolderUrl ) ;

	// Add the Folders.	
	var oNodes = fckXml.SelectNodes( 'Connector/Folders/Folder' ) ;
	var aFolders = new Array() ;
	for ( var i = 0 ; i < oNodes.length ; i++ )
	{
		aFolders.push( { name : oNodes[i].attributes.getNamedItem('name').value } ) ;
	}
	smarty.assign( 'folders' , aFolders ) ;

	// Add the Files.	
	var oNodes = fckXml.SelectNodes( 'Connector/Files/File' ) ;
	var aFiles = new Array() ;
	for ( var i = 0 ; i < oNodes.length ; i++ )
	{
		var oNode = oNodes[i] ;

		var sFileName = oNode.attributes.getNamedItem('name').value ;
		var sFileSize = oNode.attributes.getNamedItem('size').value ;
		var iCanDelete = parseInt( oNode.attributes.getNamedItem('can_delete').value ) ;
		var iFileMtime = parseInt( oNode.attributes.getNamedItem('mtime').value ) ;
		var oDate = new Date() ;
		oDate.setTime( iFileMtime * 1000 ) ;

		// Get the optional "url" attribute. If not available, build the url.
		var oFileUrlAtt = oNodes[i].attributes.getNamedItem('url') ;
		var sFileUrl = oFileUrlAtt != null ? oFileUrlAtt.value : sCurrentFolderUrl + sFileName ;

		aFiles.push( {
			name : sFileName ,
			icon : oIcons.GetIcon( sFileName ) ,
			size : sFileSize ,
			can_delete : iCanDelete ,
			mtime : iFileMtime ,
			mtime_formatted : oDate.toLocaleString() ,
			url : sFileUrl
		} ) ;
	}
	smarty.assign( 'files' , aFiles ) ;

	document.body.innerHTML = smarty.fetch( 'file:frmresourceslist.tmpl.html' ) ;
}

/* function GetFoldersAndFilesCallBack( fckXml )
{
	if ( oConnector.CheckError( fckXml ) != 0 )
		return ;

	// Get the current folder path.
	var oNode = fckXml.SelectSingleNode( 'Connector/CurrentFolder' ) ;
	var sCurrentFolderPath	= oNode.attributes.getNamedItem('path').value ;
	var sCurrentFolderUrl	= oNode.attributes.getNamedItem('url').value ;

//	var dTimer = new Date() ;

	var oHtml = new StringBuilder( '<table id="tableFiles" cellspacing="1" cellpadding="0" width="100%" border="0">' ) ;

	// Add the Folders.	
	var oNodes = fckXml.SelectNodes( 'Connector/Folders/Folder' ) ;
	for ( var i = 0 ; i < oNodes.length ; i++ )
	{
		var sFolderName = oNodes[i].attributes.getNamedItem('name').value ;
		oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/" ) ) ;
	}

	// Add the Files.	
	var oNodes = fckXml.SelectNodes( 'Connector/Files/File' ) ;
	for ( var i = 0 ; i < oNodes.length ; i++ )
	{
		var oNode = oNodes[i] ;
		var sFileName = oNode.attributes.getNamedItem('name').value ;
		var sFileSize = oNode.attributes.getNamedItem('size').value ;
		var iFileMtime = oNode.attributes.getNamedItem('mtime').value ;

		// Get the optional "url" attribute. If not available, build the url.
		var oFileUrlAtt = oNodes[i].attributes.getNamedItem('url') ;
		var sFileUrl = oFileUrlAtt != null ? oFileUrlAtt.value : sCurrentFolderUrl + sFileName ;
		
		oHtml.Append( oListManager.GetFileRowHtml( sFileName, sFileUrl, sFileSize , iFileMtime ) ) ;
	}

	oHtml.Append( '</table>' ) ;
	
	document.body.innerHTML = oHtml.ToString() ;

//	window.top.document.title = 'Finished processing in ' + ( ( ( new Date() ) - dTimer ) / 1000 ) + ' seconds' ;

} */

window.onload = function()
{
	window.top.IsLoadedResourcesList = true ;
}
	</script>
</head>
<body class="FileArea" bottommargin="10" leftmargin="10" topmargin="10" rightmargin="10">
</body>
</html>
